//This is the server part of the socket udp.
//The first step is also to get a handle of socket

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
#include<stdlib.h> 

#include<pthread.h>

#include"log.h"
#include"use_hash.h"
#include"buff_queue.h"


#define SERV_PORT   3000
#define CLIENT_PORT 6000
#define CLIENT_IP "127.0.0.1"


extern struct buff_queue *Buffer_queue;

void * dispatch()
{

	struct sockaddr_in  addr_client;
	int client_len;

	int recv_num;
	int send_num;
	
	int sock_fd;

	struct sockaddr_in  addr_serv;

	//get the handle of socket
	sock_fd = socket(AF_INET,SOCK_DGRAM,0);

	if(sock_fd < 0)
	{
        	perror("socket");
       		exit(1);
	}
	else
	{
	        printf("sock sucessful\n");
	}

	
	//init the server socket
	memset(&addr_serv,0,sizeof(struct sockaddr_in));
	addr_serv.sin_family = AF_INET;
	addr_serv.sin_port = htons(SERV_PORT);
	addr_serv.sin_addr.s_addr = htonl(INADDR_ANY);
	//init the client socket
//	memset(&addr_client,0,sizeof(struct sockaddr_in));
//	addr_client.sin_family=AF_INET;
//	addr_client.sin_port=htons(CLIENT_PORT);

//	addr_client.sin_addr.s_addr = inet_addr(CLIENT_IP);

	client_len = sizeof(struct sockaddr_in);
	//bind the port of socket and handle

	if(bind(sock_fd,(struct sockaddr *)&addr_serv,sizeof(struct sockaddr_in))<0 )
	{
        	perror("bind");
        	exit(1);
	}
	else
	{
        	printf("bind sucess\n");

	}
	
	char recv_buf[20];

	bzero(recv_buf,sizeof(recv_buf));

        int len = recvfrom(sock_fd,recv_buf,sizeof(recv_buf),0,(struct sockaddr *)&addr_client,&client_len);

        printf("receive from %s\n",inet_ntoa(addr_client.sin_addr));





	
	while(1)
	{
	//	bzero(recv_buf,sizeof(recv_buf));

          //      int len = recvfrom(sock_fd,recv_buf,sizeof(recv_buf),0,(struct sockaddr *)&addr_client,&client_len);

            //    printf("receive from %s\n",inet_ntoa(addr_client.sin_addr));
                
//		sendto(sock_fd,recv_buf,len,0,(struct sockaddr *)&addr_client,client_len);

//        	printf("begin recv:\n");
        	//receive the message of client
        	/*recv_num = recvfrom(sock_fd,recv_buf,sizeof(recv_buf),0,(struct sockaddr *)&addr_client,&client_len);

        	if(recv_num <  0)
        	{
                	printf("bad\n");
                	perror("again recvfrom");
                	exit(1);
        	}
        	else
        	{
        		recv_buf[recv_num]='\0';
        	printf("recv sucess:%s\n",recv_buf);
        	}
		*/
//		printf("now in the dispatch the packet number are      %d\n",buff_queue_count(Buffer_queue));		
//		if(Buffer_queue->count>0)
//		{

			printf("start to send packet\n");
			struct buff_queue_node *bu_queue_node;
        		bu_queue_node=buff_queue_del(Buffer_queue);

			if(bu_queue_node==NULL)
			{
				log_error("count is 0 ,but del return NULL");
				continue;
			}
			
//			sendto(sock_fd,recv_buf,len,0,(struct sockaddr *)&addr_client,client_len);

//			printf("%s\n",bu_queue_node->packet);
        		printf("begin send:\n");
			//sendto(sock_fd,recv_buf,len,0,(struct sockaddr *)&addr_client,client_len);

        		send_num = sendto(sock_fd,(void *)(bu_queue_node->packet),bu_queue_node->packet_len,0,(struct sockaddr *)&addr_client,client_len);
        		if(send_num < 0)
       			{
	        		perror("sendto");
                		exit(1);
        		}
        		else
        		{
                		printf("send sucessful\n");
				printf("and the arrive time is: %d ",bu_queue_node->arrive_time);
				printf("the packet length is %d\n",bu_queue_node->packet_len);
        		}
//		}
		
	}
	close(sock_fd);
	return;
}















